package cn.jly.spring.annotation.ioc.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.stereotype.Component;
import org.springframework.util.StringValueResolver;

/**
 * 基于xxxProcessor等beanPostProcessor实现
 *
 * @author lanyangji
 * @description 获取spring的组件
 * @date 2020/11/13 下午 4:04
 */
@Component
public class SpringComponent implements ApplicationContextAware, BeanNameAware, EmbeddedValueResolverAware {
    private ApplicationContext applicationContext;
    private String beanName;

    public void setBeanName(String name) {
        System.out.println("当前bean的名字 --> " + name);
        this.beanName = name;
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        System.out.println("applicationContext = " + applicationContext);

        final Object bean = this.applicationContext.getBean(this.beanName);
        System.out.println("---> bean = " + bean);
    }

    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        final String s = resolver.resolveStringValue("你好, ${os.name}，我是 #{20 * 10}");
        System.out.println("s = " + s);
    }
}
